﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Disposables;
using System.Linq;
using System.Linq.Expressions;
using System.Windows.Data;
using SoftSource.Common;
using SoftSource.Timecard.ViewModelInterfaces;
using SoftSource.Timecard.Web.Data;
using SoftSource.Reactive;

namespace SoftSource.Timecard.ViewModels
{
    public sealed class EntriesPriorToProjectBillingDateViewModel : IEntriesPriorToProjectBillingDateViewModel
    {
        #region Fields
        private CompositeDisposable compositeDisposable;
        #endregion Fields

        public EntriesPriorToProjectBillingDateViewModel()
        {
            this.Entries = new ObservableProperty<PagedCollectionView>();
            this.Projects = new ObservableProperty<PagedCollectionView>();
            this.compositeDisposable = new CompositeDisposable();

            var entriesDisposable = this.Entries.Subscribe(pcv =>
                {
                    if (pcv != null)
                        pcv.SortDescriptions.Add(new SortDescription("EntryDate", ListSortDirection.Descending));
                });
            var projectsDisposable = this.Projects.Subscribe(pcv =>
                {
                    if (pcv != null)
                        pcv.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));
                });
            
            this.compositeDisposable.Add(entriesDisposable);
            this.compositeDisposable.Add(projectsDisposable);
        }

        #region IEntriesPriorToProjectBillingDateViewModelConsume Members

        public IObservableProperty<PagedCollectionView> Entries { get; private set; }

        public IObservableProperty<PagedCollectionView> Projects { get; private set; }

        public IObservable<Unit> Load { get; set; }

        #endregion

        #region IEntriesPriorToProjectBillingDateViewModelService Members

        private IEnumerable<EntryLog> entrySource;
        public IEnumerable<EntryLog> EntrySource
        {
            get { return this.entrySource; }
            set
            {
                this.entrySource = value;
                this.Entries.Value = new PagedCollectionView(this.entrySource);

                var projects = from e in this.entrySource
                               join p in this.ProjectSource on e.ProjectID equals p.ProjectID
                               select p;
                this.Projects.Value = new PagedCollectionView(projects.Distinct());
            }
        }

        public IEnumerable<ProjectSummary> ProjectSource { get; set; }

        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            this.compositeDisposable.Dispose();
            GC.SuppressFinalize(this);
        }

        #endregion

        ~EntriesPriorToProjectBillingDateViewModel()
        {
            this.Dispose();
        }
    }
}
